﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

namespace CanalDeNoticiasMSDNv3.ViewModels
{
    public class ElementoEntradaVideo
    {
        /// <summary>
        /// Namespace usado para encontrar el namespace "media:"
        /// </summary>
        public static XNamespace Media = "http://search.yahoo.com/mrss/";

        public string Title { get;  set; }
        public string Video { get;  set; }
        public string Description { get;  set; }
        public string Thumbnail { get;  set; }
        public string Link { get;  set; }

        public static List<ElementoEntradaVideo> GetElements(string response)
        {
            List<ElementoEntradaVideo> elementos = new List<ElementoEntradaVideo>();

            var rssFeed= XElement.Parse(response);
            var channel=rssFeed.Descendants("channel");
            
            elementos = (from item in channel.Elements("item")
                         select new ElementoEntradaVideo
                         {
                             Title = item.Element("title").Value,
                             Description = item.Element("description").Value,
                             Video = item.Element("enclosure") == null ? null : 
                                item.Element("enclosure").Attribute("url").Value,
                                Link=item.Element("link").Value,
                             Thumbnail =
                                 (from thumb in item.Descendants(Media + "thumbnail")
                                  orderby int.Parse(thumb.Attribute("width").Value)
                                  select thumb.Attribute("url").Value).FirstOrDefault()
                         }).ToList();

            return elementos;
        }
    }
}
